import 'package:core_ui/base/page_state.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'base_logic.dart';

// ignore: must_be_immutable
abstract class BasePage<B extends BaseLogic> extends StatelessWidget {
  late B logic;
  PreferredSizeWidget? _appBar;
  Widget? _loadingView;
  Widget? _successView;
  Widget? _emptyView;
  Widget? _errorView;

  BasePage({Key? key}) : super(key: key) {
    logic = Get.put<B>(onCreateLogic());
  }

  @override
  Widget build(BuildContext context) {
    _appBar = onCreateAppbar(context);
    _loadingView = onCreateLoading(context);
    _successView = onCreateSuccess(context);
    _emptyView = onCreateEmpty(context);
    _errorView = onCreateError(context);

    if (null != _loadingView) {
      logic.loading();
    } else {
      if (null != _successView) {
        logic.success();
      } else {
        if (null != _emptyView) {
          logic.empty(1000);
        } else {
          logic.error(1000);
        }
      }
    }

    return Obx(() {
      return Scaffold(
        appBar: _appBar,
        body: _buildBody(),
      );
    });
  }

  B onCreateLogic();

  //===Desc:=====================================================================================
  Widget? _buildBody() {
    var pageState = logic.pageState.value;
    if (pageState is LoadingState) {
      return _loadingView;
    } else if (pageState is SuccessState) {
      return _successView;
    } else if (pageState is EmptyState) {
      return _emptyView;
    } else if (pageState is ErrorState) {
      return _errorView;
    } else {
      return null;
    }
  }

  //===Desc:=====================================================================================

  PreferredSizeWidget? onCreateAppbar(BuildContext context) => null;

  Widget? onCreateLoading(BuildContext context) => null;

  Widget? onCreateSuccess(BuildContext context) => null;

  Widget? onCreateEmpty(BuildContext context) => null;

  Widget? onCreateError(BuildContext context) => null;

//===Desc:=====================================================================================

}
